package com.naiyumi.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.naiyumi.dao.DownloadDao;
import com.naiyumi.dao.LoginDao;
import com.naiyumi.model.UserInfo;
import com.naiyumi.service.DownloadService;
import com.naiyumi.util.FloderNameFilter;

@Service("downloadService")
public class DownloadServiceImpl implements DownloadService {

	@Resource
	private DownloadDao dao;
	
	private String hostName = ResourceBundle.getBundle("samples").getString("host_name");
	private String basePath = ResourceBundle.getBundle("samples").getString("base_path");
	private String voicePath = ResourceBundle.getBundle("samples").getString("voice_path");
	
	/** 根据节点ID获取文件目录列表
	 * @param node
	 * @param path 
	 * @return list
	 */
	public List<Map<String, Object>> selectTreeNode(String node, String path, UserInfo userInfo) {
		if("1".equals(node)){
			path = voicePath;
		}
		List<Map<String, Object>> jsonList = new ArrayList<Map<String, Object>>();
		File f=new File(path);
		File[] list = null;
		FloderNameFilter floderNamefilter = new FloderNameFilter(userInfo.getLoginName());
		if("1".equals(node)&&!userInfo.isAdmin()){
			list = f.listFiles(floderNamefilter);
		} else {
	        list = f.listFiles();
		}
        for(int i=0;i<list.length;i++){
			Map<String, Object> m = new HashMap<String, Object>();
			m.put("text", list[i].toString().substring(list[i].toString().lastIndexOf("\\")+1, list[i].toString().length()));
			m.put("id", node+i);
			File tmp = new File(list[i].toString());
			if(tmp.isDirectory()){
				m.put("cls", "folder");
			} else {
				m.put("cls", "folder");
				m.put("leaf", "true");
				continue;
			}
			m.put("path", list[i]);
			jsonList.add(m);
        }
		return jsonList;
	}

	@SuppressWarnings({ "unused", "rawtypes", "unchecked" })
	public List<Map<String, Object>> selectFileList(String path) {
		List<Map<String, Object>> jsonList = new ArrayList<Map<String, Object>>();
		File f=new File(path);
        File[] list=f.listFiles();
        System.out.println("文件个数:"+list.length);
        if(list == null && list.length < 0){
        	return jsonList;
        }
        List<String> fileNameList = new ArrayList<String>();
        for(int i=0;i<list.length;i++){
        	if(list[i].isFile()){
        		fileNameList.add(list[i].getPath());
        	}
        }
        String[] nameList = new String[fileNameList.size()];
        for(int i = 0; i < fileNameList.size(); i++){
        	nameList[i] = fileNameList.get(i).toString();
        }
    	Arrays.sort(nameList); 
    	File[] sortedFiles = new File[nameList.length]; 
    	for (int i=0; i<nameList.length; i++) {
    		sortedFiles[i] = new File(nameList[i]);
        }
        
        for(int i=0;i<list.length;i++){
			Map<String, Object> m = new HashMap<String, Object>();
			File tmp = new File(sortedFiles[i].toString());
			if(tmp.isFile()){
				Map mm = new HashMap();
//				mm.put("id", ((i+1)%20)==0?20:((i+1)%20));
				mm.put("id", i+1);
				mm.put("name", tmp.getName());
				String filePath = tmp.getPath();
				filePath = filePath.substring(filePath.lastIndexOf(basePath)+basePath.length(), filePath.length());
				filePath = filePath.replace("\\", "/");
				filePath = filePath.replaceAll(" ", "%20");
				filePath = hostName + filePath;
//				try {
//					filePath = URLEncoder.encode(filePath, "UTF-8");
//				} catch (UnsupportedEncodingException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
				mm.put("path", filePath);
				jsonList.add(mm);
			} else {
				continue;
			}
        }
		return jsonList;
	}

	public boolean getPasswordByFloder(String floder, String password) {
		int check = this.dao.getPasswordByFloder(floder,password);
		if(check > 0){
			return true;
		} else {
			return false;
		}
	}

	public int setPasswordByFloder(String floder, String password) {
		return this.dao.setPasswordByFloder(floder, password);
	}
}
